#pragma once

#include <vector>

namespace solution_064
{

class Solution
{
public:
    int minPathSum(std::vector< std::vector<int> >& grid)
    {
        const int rows = grid.size();
        const int cols = grid.front().size();
        for (int row = 0; row < rows; ++row) {
            for (int col = 0; col < cols; ++col) {
                if (row > 0 && col > 0)
                    grid[row][col] += std::min(grid[row-1][col], grid[row][col-1]);
                else if (row > 0)
                    grid[row][col] += grid[row-1][col];
                else if (col > 0)
                    grid[row][col] += grid[row][col-1];
            }
        }
        return grid.back().back();
    }
};

}  /* namespace solution_064 */
